import {
  defineStore
} from 'pinia'

export const userStore = defineStore('user', () => {
  const userName = localStorage.getItem('_boss_login_username')
  const employeeId = localStorage.getItem('_boss_login_employeeId')
  const account = localStorage.getItem('_boss_login_account')
  const token = localStorage.getItem('_boss_token')

  const login = (employee) => {
    if(localStorage.getItem('_boss_login_username')){
      localStorage.removeItem('_boss_login_username')
    }
    localStorage.setItem('_boss_login_account', employee.employeeAccount)
    localStorage.setItem('_boss_login_username', employee.employeeName)
    localStorage.setItem('_boss_login_employeeId', employee.employeeId)
    localStorage.setItem('_boss_token', employee.token)
  }

  const logout = () => {
    localStorage.removeItem('_boss_token')
    localStorage.removeItem('_boss_menu')
  }

  return {
    userName,
    employeeId,
    account,
    token,
    login,
    logout
  }
})
